<template>
  <div class="wrap">
    <div class="imga"></div>
    <div class="userform">
      <p style="font-size:18px">{{companyName}}</p>
      <div class="username" style="margin-top:30px">
        <span>姓&nbsp;&nbsp;&nbsp;名</span>
        <input v-model.trim="username" maxlength="20" placeholder="请输入真实姓名" />
      </div>
      <div class="username" style="margin-top:25px">
        <span>身份证</span>
        <input v-model.trim="idCard" onkeyup="value=value.replace(/\s/g,'')" placeholder="请输入身份证号" />
      </div>
      <div style="height:22px"></div>
      <div class="btn" @click="handleInfo">绑定</div>
    </div>
  </div>
</template>
<script>
import { Dialog } from "vant";
import axios from "axios";
import { checkId, checkNum } from "./../utils/check";
import { loadData } from "./../utils/api";
export default {
  name: "bindUser",
  data() {
    return {
      username: "",
      idCard: "",
      mobile: "",
      companyId: "",
      companyName:""
    };
  },
  mounted() {
    this.getCompanyId();
    sessionStorage.setItem("companyId", this.companyId);
  },
  methods: {
    //提交司机信息
    handleInfo() {
      if (this.username == "") {
        
         alert("用户名不能为空")
        ;
      } else if (!checkId(this.idCard.replace(/\s*/g,""))) {
        alert("请您填写正确格式的身份证号");
      } else {
        loadData({
          method: "getShortDriver",
          dataBody: {
            name: this.username.replace(/\s*/g,""),
            companyId: this.companyId ? this.companyId : null,
            idCardNo: this.idCard.replace(/\s*/g,"")
          }
        }).then(res => {
          if (res.data.result.code == 0) {
            this.username = res.data.result.data.driverName;
            this.idCard = res.data.result.data.identityNo;
            sessionStorage.setItem("token", res.data.result.data.token);
            sessionStorage.setItem("name", this.username);
            sessionStorage.setItem("mobile", res.data.result.data.mobile);
            sessionStorage.setItem("userUuid", res.data.result.data.userUuid);
            sessionStorage.setItem("idCard", this.idCard);
            if (res.data.result.data.status == 1) {
              this.$router.push({ path: `/bindPersonalInfo` });
            } else if (res.data.result.data.status == 2) {
              this.$router.push({ path: "/bankCardInfo" });
            } else if (res.data.result.data.status == 4) {
              var companies=res.data.result.data.relations;              
              var confirmBind;
              for(let i=0;i<companies.length;i++){
                if(companies[i].logisticsUserUuid==this.companyId){
                 confirmBind=companies[i].logisticsUserUuid;   
                  break;           
                }
              }
              if(confirmBind){
                this.$router.push({ path: "/bindSuccess" });
              }else{
                this.$router.push({ path: "/bankCardInfo" });
              }
         
            } 
          } else {
              alert(res.data.result.msg);
          }
        });
      }
    },
    //获取二维码地址中的车队Id
    getCompanyId() {
     // let str = 'http://192.168.0.37:8002/?companyId=190927100352&companyName=张三#/bindSuccess'
      // let num = str.indexOf("?");
      // str = str.substring(num + 1);
      // let arr = str.split("&");
      // let obj = {};
      // for (var i = 0; i < arr.length; i++) {
      //   num = arr[i].indexOf("=");
      //   if (num > 0) {
      //     name = arr[i].substring(0, num);
      //    let value = arr[i].substr(num + 1);
      //     obj[name] = value;
      //   }
      // }
      let str = location.href;
      let start = str.indexOf("=");
      let end = str.indexOf("#");
      let sp=str.indexOf("&")
      if(start==-1){
        this.companyName="暂无车队信息"
      }else{
         this.companyId= str.substring(start + 1, sp);
         this.companyName=decodeURI(str.substring(sp+13,end))+"司机绑定"
      }
     
     

    }
  }
};
</script>
<style scoped>
.imga {
  background: url("~@/assets/bg2.png") no-repeat center;
  height: 135px;
  background-size: cover;
}
.wrap {
  position: absolute;
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
  overflow-y: auto;
  background-color: rgb(242, 242, 242);
}
input {
  border: none;
  background: none;
  outline: none;
  border-bottom: #c8cfd4 1px solid;
  width: 75%;
  margin-left: 6px;
  text-indent: 2em;
  padding-bottom: 10px;
}
.userform {
  z-index: 999;
  margin: auto;
  width: 96%;
  background: #fff;
  height: 220px;
  border-radius: 10px;
  margin-top: -80px;
}
.userform p {
  text-align: center;
  font-size: 14px;
  color: #181818;
  padding-top: 20px;
}
.username {
  width: 95%;
  margin: auto;
}
.username span {
  color: #181818;
  font-size: 16px;
}
.btn {
  margin: auto;
  width: 120px;
  height: 44px;
  border-radius: 22px;
  background: #0082d7;
  text-align: center;
  line-height: 44px;
  color: #fff;
  box-shadow: 0px 0px 12px 0px rgba(23, 153, 238, 1);
}
</style>